package cn.cslg.weatherforecast.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.cslg.weatherforecast.bean.UserBean;

public class DBDao {
    private static DBHelper mInstance = null;
    private SQLiteDatabase db;
    private String sql;

    public DBDao(Context context) {
//        mInstance = new DBHelper(context, "user.db", null, 1);
        mInstance = getInstance(context);
        db = mInstance.getReadableDatabase();//创建或打开数据库
    }

    /**
     * 单例模式
     */
    public static synchronized DBHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new DBHelper(context, "user.db", null, 1);
        }
        return mInstance;
    }

    public void insert(UserBean userBean) {
        sql = "insert into user(name,password) values(?,?)";
        db.execSQL(sql, new String[]{userBean.getName(), userBean.getPassword()});
    }

    public boolean select(UserBean userBean) {
        sql = "select * from user where name=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[]{userBean.getName(), userBean.getPassword()});
        if (cursor.getCount() == 1) {
            return true;
        }
        return false;
    }
}
